package net.cardgame.orcalecard;

import java.util.Random;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.service.ServiceDownload;
import net.cardgame.orcalecard.utils.ConstantValue;
import net.cardgame.orcalecard.utils.Utils;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

public class TestActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result_activity_layout);
		startServiceDownload();
		Random random = new Random();
		if (random.nextBoolean()) {
			startServiceDownload();
		} else {
			startServiceDownload2();
		}
	}

	private void startServiceDownload() {
		Intent intent = new Intent(this, ServiceDownload.class);
		intent.putExtra(ServiceDownload.KEY_PATH_SERVER,
				"http://www.jma-inc.jp/appdata/OracleCard/Card_03.zip");
		intent.putExtra(ServiceDownload.KEY_DECK_ID, 3);
		intent.putExtra(ServiceDownload.KEY_POSITION, 13);
		intent.putExtra(ServiceDownload.KEY_PATH_SAVE,
				ConstantValue.getPatchCardData(this) + "Card_03.zip");
		startService(intent);
	}

	private void startServiceDownload2() {
		Intent intent = new Intent(this, ServiceDownload.class);
		intent.putExtra(ServiceDownload.KEY_PATH_SERVER,
				"http://www.jma-inc.jp/appdata/OracleCard/Card_04.zip");
		intent.putExtra(ServiceDownload.KEY_DECK_ID, 4);
		intent.putExtra(ServiceDownload.KEY_POSITION, 12);
		intent.putExtra(ServiceDownload.KEY_PATH_SAVE,
				ConstantValue.getPatchCardData(this) + "Card_04.zip");
		startService(intent);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// Register mMessageReceiver to receive messages.
		LocalBroadcastManager.getInstance(this).registerReceiver(
				mBroadcastReceiver,
				new IntentFilter(ConstantValue.ACTION_DOWNLOAD));
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		LocalBroadcastManager.getInstance(this).unregisterReceiver(
				mBroadcastReceiver);
		super.onPause();
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Bundle bundle = intent.getExtras();
			int deckID = bundle.getInt(ServiceDownload.KEY_DECK_ID);
			int position = bundle.getInt(ServiceDownload.KEY_POSITION);
			int percent = bundle.getInt(ServiceDownload.KEY_PERCENT);
			if (bundle.containsKey(ServiceDownload.KEY_STATUS)) {
				boolean finished = bundle
						.getBoolean(ServiceDownload.KEY_STATUS);
				Utils.ELog("Download and Unzip status", finished ? "finished"
						: "unfinished");
			}

			Utils.ELog("receiver", "deckId:" + deckID + "   position:"
					+ position + "  percent:" + percent);

		}
	};
}
